1
|
|
|
'use strict'; |
2
|
|
|
|
3
|
|
|
var expect = require('expect.js'); |
4
|
|
|
|
5
|
|
|
var Position = require('../lib/position'); |
6
|
|
|
var Grid = require('../lib/grid'); |
7
|
|
|
var Mower = require('../lib/mower'); |
8
|
|
|
var Move = require('../lib/move'); |
9
|
|
|
|
10
|
|
|
describe('Move', function() { |
11
|
|
|
describe('runInstruction', function() { |
12
|
|
|
var grid = new Grid(new Position({x:5,y:5}, 'N')), |
13
|
|
|
position = new Position({x:2,y:2}, 'E'), |
14
|
|
|
mower = new Mower(0, grid, position, ['D']), |
15
|
|
|
move = new Move(mower, grid); |
16
|
|
|
|
17
|
|
|
it('should run instruction ’A’ with cardinal ’E’ and change mower’s position to: x+1', function (done) { |
18
|
|
|
move.runInstruction('A').then(function(newPosition) { |
19
|
|
|
expect(newPosition.x).to.equal(position.x+1); |
20
|
|
|
done(); |
21
|
|
|
}); |
22
|
|
|
}); |
23
|
|
|
it('should run instruction ’A’ with cardinal ’W’ and change mower’s position to: x-1', function (done) { |
24
|
|
|
var startPosition = new Position({x:2,y:2}, 'W'); |
25
|
|
|
move.mower.position = startPosition; |
26
|
|
|
move.runInstruction('A').then(function(newPosition) { |
27
|
|
|
expect(newPosition.x).to.equal(startPosition.x-1); |
28
|
|
|
done(); |
29
|
|
|
}); |
30
|
|
|
}); |
31
|
|
|
it('should run instruction ’A’ with cardinal ’N’ and change mower’s position to: y+1', function (done) { |
32
|
|
|
var startPosition = new Position({x:2,y:2}, 'N'); |
33
|
|
|
move.mower.position = startPosition; |
34
|
|
|
move.runInstruction('A').then(function(newPosition) { |
35
|
|
|
expect(newPosition.y).to.equal(startPosition.y+1); |
36
|
|
|
done(); |
37
|
|
|
}); |
38
|
|
|
}); |
39
|
|
|
it('should run instruction ’A’ with cardinal ’S’ and change mower’s position to: y-1', function (done) { |
40
|
|
|
var startPosition = new Position({x:2,y:2}, 'S'); |
41
|
|
|
move.mower.position = startPosition; |
42
|
|
|
move.runInstruction('A').then(function(newPosition) { |
43
|
|
|
expect(newPosition.y).to.equal(startPosition.y-1); |
44
|
|
|
done(); |
45
|
|
|
}); |
46
|
|
|
}); |
47
|
|
|
it('should run instruction ’G’ with cardinal ’N’ and change mower’s position to: c=’W’', function (done) { |
48
|
|
|
move.mower.position = new Position({x:2,y:2}, 'N'); |
49
|
|
|
move.runInstruction('G').then(function(newPosition) { |
50
|
|
|
expect(newPosition.c).to.equal('W'); |
51
|
|
|
done(); |
52
|
|
|
}); |
53
|
|
|
}); |
54
|
|
|
it('should run instruction ’G’ with cardinal ’S’ and change mower’s position to: c=’E’', function (done) { |
55
|
|
|
move.mower.position = new Position({x:2,y:2}, 'S'); |
56
|
|
|
move.runInstruction('G').then(function(newPosition) { |
57
|
|
|
expect(newPosition.c).to.equal('E'); |
58
|
|
|
done(); |
59
|
|
|
}); |
60
|
|
|
}); |
61
|
|
|
it('should run instruction ’D’ with cardinal ’N’ and change mower’s position to: c=’E’', function (done) { |
62
|
|
|
move.mower.position = new Position({x:2,y:2}, 'N'); |
63
|
|
|
move.runInstruction('D').then(function(newPosition) { |
64
|
|
|
expect(newPosition.c).to.equal('E'); |
65
|
|
|
done(); |
66
|
|
|
}); |
67
|
|
|
}); |
68
|
|
|
it('should run instruction ’D’ with cardinal ’S’ and change mower’s position to: c=’W’', function (done) { |
69
|
|
|
move.mower.position = new Position({x:2,y:2}, 'S'); |
70
|
|
|
move.runInstruction('D').then(function(newPosition) { |
71
|
|
|
expect(newPosition.c).to.equal('W'); |
72
|
|
|
done(); |
73
|
|
|
}); |
74
|
|
|
}); |
75
|
|
|
it('should throw an error when instruction is not valid', function (done) { |
76
|
|
|
move.mower.position = new Position({x:2,y:2}, 'S'); |
77
|
|
|
move.runInstruction('U').catch(function(err) { |
78
|
|
|
expect(err).to.be.a(Error); |
79
|
|
|
done(); |
80
|
|
|
}); |
81
|
|
|
}); |
82
|
|
|
}); |
83
|
|
|
describe('updatePosition', function() { |
84
|
|
|
var grid = new Grid(new Position({x:7,y:7}, 'N')), |
85
|
|
|
position = new Position({x:2,y:2}, 'W'), |
86
|
|
|
mower = new Mower(0, grid, position, ['G']), |
87
|
|
|
move = new Move(mower, grid); |
88
|
|
|
|
89
|
|
|
it('should update a position to x+1', function () { |
90
|
|
|
var newPosition = new Position({x:position.x+1,y:position.y}, 'W'); |
91
|
|
|
move.updatePosition(newPosition); |
92
|
|
|
expect(move.mower.position.x).to.equal(position.x+1); |
93
|
|
|
}); |
94
|
|
|
it('should update a position to x-1', function () { |
95
|
|
|
var newPosition = new Position({x:position.x-1,y:position.y}, 'W'); |
96
|
|
|
move.updatePosition(newPosition); |
97
|
|
|
expect(move.mower.position.x).to.equal(position.x-1); |
98
|
|
|
}); |
99
|
|
|
it('should update a position to y+1', function () { |
100
|
|
|
var newPosition = new Position({x:position.x,y:position.y+1}, 'W'); |
101
|
|
|
move.updatePosition(newPosition); |
102
|
|
|
expect(move.mower.position.y).to.equal(position.y+1); |
103
|
|
|
}); |
104
|
|
|
it('should update a position to y-1', function () { |
105
|
|
|
var newPosition = new Position({x:position.x,y:position.y-1}, 'W'); |
106
|
|
|
move.updatePosition(newPosition); |
107
|
|
|
expect(move.mower.position.y).to.equal(position.y-1); |
108
|
|
|
}); |
109
|
|
|
}); |
110
|
|
|
describe('isNextPositionAllowed', function() { |
111
|
|
|
var grid = new Grid(new Position({x:7,y:7}, 'N')), |
112
|
|
|
position = new Position({x:2,y:2}, 'E'), |
113
|
|
|
mower = new Mower(0, grid, position, ['G']), |
114
|
|
|
move = new Move(mower, grid); |
115
|
|
|
|
116
|
|
|
it('should return false for testing a position x:20 and y:-20', function () { |
117
|
|
|
var newPosition = new Position({x:20,y:-20}, 'E'); |
118
|
|
|
expect(move.isNextPositionAllowed(newPosition)).to.equal(false); |
119
|
|
|
}); |
120
|
|
|
it('should return false for testing a position x:-20 and y:20', function () { |
121
|
|
|
var newPosition = new Position({x:-20,y:20}, 'E'); |
122
|
|
|
expect(move.isNextPositionAllowed(newPosition)).to.equal(false); |
123
|
|
|
}); |
124
|
|
|
it('should return false for testing a x:5 and y:-20', function () { |
125
|
|
|
var newPosition = new Position({x:5,y:-20}, 'E'); |
126
|
|
|
expect(move.isNextPositionAllowed(newPosition)).to.equal(false); |
127
|
|
|
}); |
128
|
|
|
it('should return false for testing a x:-20 and y:5', function () { |
129
|
|
|
var newPosition = new Position({x:-20,y:5}, 'E'); |
130
|
|
|
expect(move.isNextPositionAllowed(newPosition)).to.equal(false); |
131
|
|
|
}); |
132
|
|
|
}); |
133
|
|
|
describe('goForward', function() { |
134
|
|
|
var grid = new Grid(new Position({x:6,y:6}, 'N')), |
135
|
|
|
position = new Position({x:2,y:2}, 'E'), |
136
|
|
|
mower = new Mower(0, grid, position, ['A']), |
137
|
|
|
move = new Move(mower, grid); |
138
|
|
|
|
139
|
|
|
it('should move mower to 1 cell forward with cardinal ’E’ and change mower’s position to: x+1', function () { |
140
|
|
|
move.goForward(); |
141
|
|
|
expect(move.mower.position.x).to.equal(position.x+1); |
142
|
|
|
}); |
143
|
|
|
it('should move mower to 1 cell forward with cardinal ’W’ and change mower’s position to: x-1', function () { |
144
|
|
|
move.mower.position = new Position({x:2,y:2}, 'W'); |
145
|
|
|
move.goForward(); |
146
|
|
|
expect(move.mower.position.x).to.equal(position.x-1); |
147
|
|
|
}); |
148
|
|
|
it('should move mower to 1 cell forward with cardinal ’N’ and change mower’s position to: y+1', function () { |
149
|
|
|
move.mower.position = new Position({x:2,y:2}, 'N'); |
150
|
|
|
move.goForward(); |
151
|
|
|
expect(move.mower.position.y).to.equal(position.y+1); |
152
|
|
|
}); |
153
|
|
|
it('should move mower to 1 cell forward with cardinal ’S’ and change mower’s position to: y-1', function () { |
154
|
|
|
move.mower.position = new Position({x:2,y:2}, 'S'); |
155
|
|
|
move.goForward(); |
156
|
|
|
expect(move.mower.position.y).to.equal(position.y-1); |
157
|
|
|
}); |
158
|
|
|
}); |
159
|
|
|
describe('turn', function() { |
160
|
|
|
var grid = new Grid(new Position({x:10,y:10}, 'N')), |
161
|
|
|
position = new Position({x:2,y:2}, 'E'), |
162
|
|
|
mower = new Mower(0, grid, position, ['G']), |
163
|
|
|
move = new Move(mower, grid); |
164
|
|
|
|
165
|
|
|
it('should move mower to the left with cardinal ’E’ and change mower’s position to: c=’N’', function () { |
166
|
|
|
move.turn('G'); |
167
|
|
|
expect(move.mower.position.c).to.equal('N'); |
168
|
|
|
}); |
169
|
|
|
it('should move mower to the left with cardinal ’N’ and change mower’s position to: c=’W’', function () { |
170
|
|
|
move.mower.position = new Position({x:2,y:2}, 'N'); |
171
|
|
|
move.turn('G'); |
172
|
|
|
expect(move.mower.position.c).to.equal('W'); |
173
|
|
|
}); |
174
|
|
|
it('should move mower to the right with cardinal ’E’ and change mower’s position to: c=’S’', function () { |
175
|
|
|
move.mower.position = new Position({x:2,y:2}, 'E'); |
176
|
|
|
move.turn('D'); |
177
|
|
|
expect(move.mower.position.c).to.equal('S'); |
178
|
|
|
}); |
179
|
|
|
it('should move mower to the right with cardinal ’W’ and change mower’s position to: c=’N’', function () { |
180
|
|
|
move.mower.position = new Position({x:2,y:2}, 'W'); |
181
|
|
|
move.turn('D'); |
182
|
|
|
expect(move.mower.position.c).to.equal('N'); |
183
|
|
|
}); |
184
|
|
|
it('should throw an error when instruction is not valid', function () { |
185
|
|
|
move.mower.position.c = 'N'; |
186
|
|
|
expect(function() { |
187
|
|
|
move.turn('U'); |
188
|
|
|
}).to.throwError(); |
189
|
|
|
}); |
190
|
|
|
}); |
191
|
|
|
}); |